local skill = fk.CreateSkill{
    name = "lingling__roushencaijianpan",
}
Fk:loadTranslationTable{
    ["lingling__roushencaijianpan"] = "肉神踩键盘",
    [":lingling__roushencaijianpan"] = "出牌阶段限一次，你可以弃置一张牌并指定一名其他角色，其下回合对其以外的角色使用牌时目标随机。",
    ["@[desc]lingling__caijianpan"] = "被踩键盘",
    [":lingling__caijianpan"] = "对自己以外的角色使用牌时目标随机",
}
skill:addEffect("active",{
    anim_type = "control",
    max_phase_use_time = 1,
    can_use = function (self, player)
        return self:withinTimesLimit(player,Player.HistoryPhase)
    end,
    card_num = 1,
    card_filter = function (self, player, to_select, selected, selected_targets)
        return not player:prohibitDiscard(to_select)
    end,
    target_num = 1,
    target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
        return #selected == 0 and to_select ~= player
    end,
    on_use = function (self, room, effect)
        local player = effect.from
        local to = effect.tos[1]
        room:throwCard(effect.cards,skill.name,player)
        room:setPlayerMark(to,"@[desc]lingling__caijianpan",1)
    end,
})
skill:addEffect(fk.TurnEnd,{
    global = true,
    can_refresh = function (self, event, target, player, data)
        return player == target and player:getMark("@[desc]lingling__caijianpan") ~= 0
    end,
    on_refresh = function (self, event, target, player, data)
        player.room:setPlayerMark(player,"@[desc]lingling__caijianpan",0)
    end,
})
skill:addEffect(fk.PreCardUse,{
    global = true,
    can_refresh = function (self, event, target, player, data)
        return player == target and player:getMark("@[desc]lingling__caijianpan") ~= 0 and not (data:isOnlyTarget(data.tos[1]) and data.tos[1] == player)
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        local orig_tos = table.simpleClone(data.tos)
        -- 先删光原目标再说
        data:removeAllTargets()
        local extra_data = {bypass_distances = true, bypass_times = true}
        local targets = table.filter(room.alive_players, function (to)
            return not player:isProhibited(to, data.card) and
            -- 延时锦囊可以贴自己头上……
            (data.card.sub_type == Card.SubtypeDelayedTrick or data.card.skill:modTargetFilter(data.from, to, {}, data.card, extra_data))
        end)
        -- 用于记录每个目标合法的副目标
        local sub_targets_map = {}
        -- 如果此牌需要副目标，那还得筛选副目标合法性
        if data.card.skill:getMinTargetNum(data.from) > 1 then
            for i = #targets, 1, -1 do
                local to = targets[i]
                local valid_sub_tos = table.filter(room:getOtherPlayers(to, false), function (to2)
                    return data.card.skill:targetFilter(data.from, to2, {to}, data.card.subcards, data.card, extra_data)
                end)
                if #valid_sub_tos > 0 then
                    sub_targets_map[to] = valid_sub_tos
                else
                    table.remove(targets, i)
                end
            end
        end
        -- 没有合法目标则取消使用
        if #targets == 0 then
            return false
        end
        -- 单目标的牌则随机选1个新目标
        if #orig_tos == 1 then
            local random_target = table.random(targets)
            -- 转圈圈动画
            if #targets > 1 then
                for i = 1, 2, 1 do
                    for _, p in ipairs(room:getAllPlayers()) do
                        room:setEmotion(p, "./image/anim/selectable")
                        room:notifyMoveFocus(p, skill.name)
                        room:delay(300)
                    end
                end
                for _, p in ipairs(room:getAllPlayers()) do
                    if table.contains(targets, p) then
                        room:setEmotion(p, "./image/anim/selectable")
                        room:delay(500)
                        if p == random_target then
                            room:doIndicate(data.from, {random_target})
                            break
                        end
                    end
                end
            end
            local sub_to = sub_targets_map[random_target] and {table.random(sub_targets_map[random_target])} or nil
            data:addTarget(random_target, sub_to)
        else
            -- 若原目标数为X>1，则随机选择1~X个新目标
            local new_targets = table.random(targets, math.random(1, #orig_tos))
            for _, to in ipairs(new_targets) do
                local sub_to = sub_targets_map[to] and {table.random(sub_targets_map[to])} or nil
                data:addTarget(to, sub_to)
            end
        end
    end,
})
return skill